package com.icss.ah.base.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.baidu.ueditor.ConfigManager;
import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.State;
import com.baidu.ueditor.hunter.FileManager;
import com.baidu.ueditor.hunter.ImageHunter;

public class ActionEnter
{
  private HttpServletRequest request = null;

  private String rootPath = null;
  private String contextPath = null;
  private String flag = null;
  private JSONObject dataJson = null;

  private String actionType = null;

  private ConfigManager configManager = null;

  public ActionEnter(HttpServletRequest request, String rootPath)
  {
    this.request = request;
//    try{
//		  this.request.setCharacterEncoding("utf-8");
//		  BufferedReader in=new BufferedReader(new InputStreamReader(this.request.getInputStream(),"utf-8"));
//		  StringBuilder sb = new StringBuilder();   
//		  String line = null;  
//		  while ((line = in.readLine()) != null) {   
//			  sb.append(line);   
//		  }
//		  System.out.println("==============sb:"+sb.toString());
//	  } catch (Exception e) {
//		e.printStackTrace();
//	  }
    this.rootPath = rootPath;
    this.actionType = request.getParameter("action");
    this.flag = request.getParameter("flag");
    this.flag = this.flag == null ? "" : this.flag;
    this.contextPath = request.getContextPath();
    this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath, request.getRequestURI());
  }

  public String exec()
  {
    String callbackName = this.request.getParameter("callback");
System.out.println("====callbackName:+actionEnter-39:"+callbackName);
    if (callbackName != null)
    {
      if (!validCallbackName(callbackName)) {
        return new BaseState(false, 401).toJSONString();
      }

      return callbackName + "(" + invoke() + ");";
    }

    return invoke();
  }

  public String invoke()
  {
	  
    if ((this.actionType == null) || (!ActionMap.mapping.containsKey(this.actionType))) {
      return new BaseState(false, 101).toJSONString();
    }

    if ((this.configManager == null) || (!this.configManager.valid())) {
      return new BaseState(false, 102).toJSONString();
    }

    State state = null;

    int actionCode = ActionMap.getType(this.actionType);

    Map conf = null;
    System.out.println(actionType+":actionType==actionCode==actionEnter-64:"+actionCode);
    switch (actionCode)
    {
    case 0:
      return this.configManager.getAllConfig().toString();
    case 1:
    case 2:
    case 3:
    case 4:
      conf = this.configManager.getConfig(actionCode);
      System.out.println("==actionEnter-74==savePath:"+conf.get("savePath"));
      state = new Uploader(this.request, conf).doExec();
      break;
    case 5:
//      if(this.flag.equals("up")){
//    	  String source = this.request.getParameter("source");
//    	  source = source == null ? "" : source;
//    	  try {
//    		  if(!source.equals("")){
//    			  state = new Uploader_up(this.request,source).doExec();
//    		  }
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//      } else {
//    	  conf = this.configManager.getConfig(actionCode);
//    	  String[] list = this.request.getParameterValues((String)conf.get("fieldName"));
//    	  state = new ImageHunter(conf).capture(list);
//      }
    conf = this.configManager.getConfig(actionCode);
  	  String[] list = this.request.getParameterValues((String)conf.get("fieldName"));
  	  state = new ImageHunter(conf).capture(list);
    	
      break;
    case 6:
    case 7:
      conf = this.configManager.getConfig(actionCode);
      int start = getStartIndex();
      state = new FileManager(conf).listFile(start);
    }

    return state.toJSONString();
  }

  public int getStartIndex()
  {
    String start = this.request.getParameter("start");
    try
    {
      return Integer.parseInt(start); } catch (Exception e) {
    }
    return 0;
  }

  public boolean validCallbackName(String name)
  {
    return name.matches("^[a-zA-Z_]+[\\w0-9_]*$");
  }
}